package com.tiger.project.service.impl;
import java.util.List;

import com.tiger.common.utils.DateUtils;
import com.tiger.project.domain.*;
import com.tiger.project.mapper.*;
import com.tiger.project.service.IProjectArchiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 【项目档案】Service业务层处理
 *
 * @author standard
 * @date 2022-09-19
 */
@Service
public class ProjectArchiveServiceImpl implements IProjectArchiveService {
    @Autowired
    private ProjectArchiveMapper projectArchiveMapper;
    @Autowired
    private MaintenanceApplyMapper maintenanceApplyMapper;
    @Autowired
    private ConstructionApplyMapper constructionApplyMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private ProjectAcceptanceMapper projectAcceptanceMapper;
    @Autowired
    private MaintenanceEnclosureMapper maintenanceEnclosureMapper;
    @Autowired
    private ConstructionEnclosureMapper constructionEnclosureMapper;

    /**
     * 查询【项目档案】
     *
     * @param id 【项目档案】主键
     * @return 【项目档案】
     */
    @Override
    public ProjectArchive selectProjectArchiveById(Long id) {
        ProjectArchive projectArchive = projectArchiveMapper.selectProjectArchiveById(id);
        if (projectArchive.getProjectType() == 1L){ //维修项目
            //项目信息
            MaintenanceApply maintenanceApply = new MaintenanceApply();
            maintenanceApply.setId(projectArchive.getProjectId());
            projectArchive.setMaintenanceApplyList( maintenanceApplyMapper.selectMaintenanceApplyList(maintenanceApply));
            //附件
            MaintenanceEnclosure maintenanceEnclosure = new MaintenanceEnclosure();
            maintenanceEnclosure.setMaintenanceApplyId(projectArchive.getProjectId());
            projectArchive.getMaintenanceApplyList().forEach(item ->{
                item.setEnclosureList(maintenanceEnclosureMapper.selectMaintenanceEnclosureList(maintenanceEnclosure));
            });
        }else {
            ConstructionApply constructionApply = new ConstructionApply();
            constructionApply.setId(projectArchive.getId());
            projectArchive.setConstructionApplyList(constructionApplyMapper.selectConstructionApplyList(constructionApply));
            //附件
            ConstructionEnclosure constructionEnclosure = new ConstructionEnclosure();
            constructionEnclosure.setConstructionApplyId(projectArchive.getProjectId());
            projectArchive.getConstructionApplyList().forEach(info ->{
                info.setEnclosureList(constructionEnclosureMapper.selectConstructionEnclosureList(constructionEnclosure));
            });
        }
        selectInfo(projectArchive);
        return projectArchive;
    }

    /**
     * 查询【项目档案】列表
     *
     * @param projectArchive 【项目档案】
     * @return 【项目档案】
     */
    @Override
    public List<ProjectArchive> selectProjectArchiveList(ProjectArchive projectArchive) {
        return projectArchiveMapper.selectProjectArchiveList(projectArchive);
    }

    /**
     * 新增【项目档案】
     *
     * @param projectArchive 【项目档案】
     * @return 结果
     */
    @Override
    public int insertProjectArchive(ProjectArchive projectArchive) {
        return projectArchiveMapper.insertProjectArchive(projectArchive);
    }

    /**
     * 修改【项目档案】
     *
     * @param projectArchive 【项目档案】
     * @return 结果
     */
    @Override
    public int updateProjectArchive(ProjectArchive projectArchive) {
        projectArchive.setIsArchived(1L);
        projectArchive.setUpdatedAt(DateUtils.getNowDate());
        return projectArchiveMapper.updateProjectArchive(projectArchive);
    }

    /**
     * 批量删除【项目档案】
     *
     * @param ids 需要删除的【项目档案】主键
     * @return 结果
     */
    @Override
    public int deleteProjectArchiveByIds(Long[] ids) {
        return projectArchiveMapper.deleteProjectArchiveByIds(ids);
    }

    /**
     * 删除【项目档案】信息
     *
     * @param id 【项目档案】主键
     * @return 结果
     */
    @Override
    public int deleteProjectArchiveById(Long id) {
        return projectArchiveMapper.deleteProjectArchiveById(id);
    }


    private void selectInfo(ProjectArchive projectArchive){
        //合同
        Contract contract = new Contract();
        contract.setApplyId(projectArchive.getProjectId());
        List<Contract> list = contractMapper.selectContractList(contract);
        projectArchive.setContractList(list);

        //项目验收
        for (Contract c : list){
            ProjectAcceptance acceptance = new ProjectAcceptance();
            acceptance.setContractId(c.getId());
            projectArchive.setAcceptanceList(projectAcceptanceMapper.selectProjectAcceptanceList(acceptance));
        }
    }

}
